<?php
class Input
{
    const UINT          = 'uint';
    const INT           = 'int';
    const BINARY        = 'binary';
    const FLOAT         = 'float';
    const STRING        = 'string';
    const ARRAY_SIMPLE  = 'array_simple';

    protected $_requests = array();

    public function __construct()
    {
        $this->_requests = array_merge($_COOKIE, $_REQUEST);
    }

    public function filter(array $inputs)
    {
        $cleanedInputs = array();

        foreach ($inputs as $input => $type)
        {
            $cleanedInputs[$input] = $this->filterSingle($input, $type);
        }

        return $cleanedInputs;
    }

    public function filterSingle($input, $type)
    {
        if ($this->inRequest($input))
        {
            return $this->cleanInput($input, $type);
        }

        return false;
    }

    public function inRequest($input)
    {
        return isset($this->_requests[$input])? true : false;
    }

    public function isPostRequest()
    {
        return ($_SERVER['REQUEST_METHOD'] == 'post')? true : false;
    }

    public function isAjaxRequest()
    {
        if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
            strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
        {
            return true;
        }

        return false;
    }

    public function cleanInput($input, $type)
    {
        $cleanedInput = null;

        $input = $this->_requests[$input];

        switch ($type)
        {
            case self::INT:
                $cleanedInput = intval($input);
                break;
            case self::UINT:
                $cleanedInput = ($t = intval($input)) < 0? 0 : $t;
                break;
            case self::BINARY:
                $cleanedInput = strval($input);
                break;
            case self::FLOAT:
                $cleanedInput = floatval($input);
                break;
            case self::STRING:
                $cleanedInput = strval($input);
                break;
            case self::ARRAY_SIMPLE:
                $cleanedInput = is_array($input)? $input : array();
                break;
        }

        return $cleanedInput;
    }
}